package com.xiaoma.initializer;

import com.xiaoma.observer.SendChannels;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.lang.NonNull;

import java.util.HashMap;
import java.util.Map;

/**
 * @description  
 * @author wangtianxiang
 * @date: 2021/11/23 15:03
 */
@Slf4j
@Order(-1)
public class DemoApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Autowired
    private SendChannels sendChannels;

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        log.info("Hello, TenmaoInitializer For SpringBoot!");
        //读取环境变量
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String name = environment.getProperty("name", "application-name");
        log.info("name: {}", name);

        //读取bean信息
        String[] definitionNames = applicationContext.getBeanFactory().getBeanDefinitionNames();
        for (String definitionName : definitionNames) {
            log.info("{}", definitionName);
        }

        //加载其他配置信息源，这里使用一个map来作为配置信息源，实际使用中是可以从远程网络中获取
        Map<String, String> config = new HashMap<>(4);
        config.put("age", "11");
        environment.getPropertySources().addFirst(new PropertySource<Map<String, String>>("ten_config", config) {
            @Override
            public String getProperty(@NonNull String name) {
                return getSource().get(name);
            }
        });
    }
}
